<template>
    <view class="jk-table-container">
        <scroll-view scroll-x="true" class="header-cell-item" :scroll-left="scrollLeft">
            <view class="cell-item">
                <view
                    v-for="(headerItem, headerIndex) in tableHeader"
                    :key="headerIndex"
                    :class="[
                    'cell-item-cell',
                    headerItem.width || 'flex-1'
                ]"
                    :style="{ width: headerItem.width ? headerItem.width : 'auto' }"
                >
                    <view class="cell-item-cell-inner" :style="{justifyContent: setCellAlign(headerItem)}" v-if="headerItem.type && headerItem.type === 'seq'">{{ headerItem.title }}</view>
                    <view class="cell-item-cell-inner" :style="{justifyContent: setCellAlign(headerItem)}" v-else >{{ headerItem.title }}</view>
                </view>
            </view>
        </scroll-view>
        <scroll-view scroll-y="true" scroll-x="true" style="height: 240px;">
            <view
                v-for="(dataItem, dataIndex) in tableData"
                :key="dataIndex"
                class="cell-item data-cell-item"
            >
                <view
                    v-for="(headerItem, headerIndex) in tableHeader"
                    :key="headerIndex"
                    :class="[
                        'cell-item-cell',
                        headerItem.width || 'flex-1'
                    ]"
                    :style="{ width: headerItem.width ? headerItem.width : 'auto' }"
                >
                    <view class="cell-item-cell-inner" :style="{justifyContent: setCellAlign(headerItem)}" v-if="headerItem.type && headerItem.type === 'seq'">{{ setRankingIcon(dataIndex) }}</view>
                    <view class="cell-item-cell-inner" :style="{justifyContent: setCellAlign(headerItem)}" v-else>{{ dataItem[headerItem.key] }}</view>
                </view>
            </view>
        </scroll-view>
    </view>
</template>

<script>
export default {
    name: 'JkTableList',
    props: {
        height: {
            type: [Number], // px
            default: 0
        },
        tableHeader: {
            type: Array,
            default() {
                return [
                    // { type: 'seq', title: '序号', width: '60px' },
                    // { key: 'machineCode', title: '机台', width: '' },
                    // { key: 'runningEfficiency', title: '效率', width: '' },
                ]
            }
        },
        tableData: {
            type: Array,
            default() {
                return []
            }
        },
        stripe: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            columnList: [],
            showPopup: false,
            scrollLeft: 0
        };
    },
    methods: {
        setRankingIcon(rowIndex) {
            let icon = '';
            if (rowIndex === 0) {
                icon = '🏆';
            } else if (rowIndex === 1) {
                icon = '🥈';
            } else if (rowIndex === 2) {
                icon = '🥉';
            } else {
                icon = rowIndex;
            }
            return icon;
        },
        onBodyScroll(e) {
            if (e.detail.scrollLeft !== this.scrollLeft) {
                this.scrollLeft = e.detail.scrollLeft;
            }
        },
        setCellAlign(headerItem) {
            let str = headerItem.align === 'center' ? 'center' : headerItem.align === 'right' ? 'flex-end' : 'flex-start';
            return str;
        },
        onRow(e) {
            this.$emit('row', e);
        },
        onPull() {
            this.$emit('pull');
        },
        pullLoading(done) {
            done();
        },
        onAllSelection(e, arr) {
            this.$emit('checked', arr);
        },
        toggleAllSelection(e, arr) {
            this.$emit('checked', arr);
        }
    },
    watch: {
        tableHeader: {
            handler(newVal) {
                this.columnList = newVal.map(x => {
                    return x;
                });
            },
            deep: true,
            immediate: true
        }
    }
}
</script>
<style scoped lang="scss">
.jk-table-container {
    //border: solid 1rpx #e8eaec;
    border-radius: 14rpx;
    overflow: hidden;
    .header-cell-item {
        position: sticky;
        color: #808695;
        font-size: 28rpx;
    }
    .data-cell-item {
        color: #808695;
        //background: #f8f8f9;
        font-size: 30rpx;
    }
    .cell-item {
        height: 68rpx;
        display: flex;
        align-items: center;
        color: #515a6e;
        .cell-item-cell {
            height: 100%;
            flex-shrink: 0;
            //border-right: solid 1rpx #e8eaec;
            border-bottom: solid 1rpx #f8f8f9;
            .cell-item-cell-inner {
                display: flex;
                align-items: center;
                height: 100%;
                padding: 4rpx 8rpx;
                box-sizing: border-box;
                word-break: break-all;
            }
        }
        .flex-1 {
            flex: 1;
        }
    }
}
</style>
